import 'package:flutter/material.dart';
import 'package:hello_flutter/widget_with_padding.dart';
import 'recycler_widget.dart';

class RecyclerViewDemo extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    List<int> dataList = List<int>();
    for (int i = 0; i < 20; i++) {
      dataList.add(i);
    }

    //自定义列表
    var recyclerView = RecyclerView(dataList, 3, 2.0, BindViewImpl());

    return Scaffold(
        appBar: AppBar(
          title: Text("RecyclerViewDemo"),
        ),
        body: recyclerView
    );
  }
}

class BindViewImpl extends ViewBinder {
  @override
  bindView(context, index, dataList) {
    //需要返回一个widget
    return WidgetWithPadding(
        FlatButton(
            color: Colors.blue[200],
            child: Text("${dataList[index]}", style: TextStyle(
                fontFamily: "Courier",
                color: Colors.white
            )),
            onPressed: () {
              //点击事件
              //...
              //更新列表元素
              //recyclerView.notifyDataChange(void Function())
              recyclerView.notifyDataChange(() {
                dataList.removeAt(index);
              });
            }
        ), 2, 2, 2, 2
    );
  }
}